﻿using System;
using System.Collections.Generic;
using System.Web.Routing;
using System.Text;

namespace Htss.Platform.Web.Http
{
    public static class HttpRouteCollectionExtension
    {
        public static Route MapRouteBox(this RouteCollection routes, string name, string url, object defaults)
        {
            return MapRouteBox(routes, name, url, defaults, null, null);
        }
        public static Route MapRouteBox(this RouteCollection routes, string name, string url, object defaults,object constraints)
        {
            return MapRouteBox(routes, name, url, defaults, constraints, null);
        }
        public static Route MapRouteBox(this RouteCollection routes, string name, string url, object defaults, string foldername)
        {
            return MapRouteBox(routes, name, url, defaults, null, foldername);
        }
        /// <summary>
        /// 添加路由(路由按从上到下匹配)
        /// </summary>
        /// <param name="routes">RouteCollection对象</param>
        /// <param name="name">路由名称(只能是唯一)</param>
        /// <param name="url">路由地址</param>
        /// <param name="defaults">默认参数</param>
        /// <param name="constraints">URL有效参数制定有效值得表达式的词典</param>
        /// <param name="foldername">文件夹名</param>
        /// <returns></returns>
        public static Route MapRouteBox(this RouteCollection routes, string name, string url, object defaults, object constraints, string foldername)
        {
            if (routes == null)
            {
                throw new ArgumentNullException("routes");
            }
            if (url == null)
            {
                throw new ArgumentNullException("url");
            }
            // 在这里注册 Route 与 HttpRouteHandler的映射关系
            Route route = new Route(url, new HttpRouteHandler())
            {
                Defaults = new RouteValueDictionary(defaults),
                Constraints = new RouteValueDictionary(constraints),
                DataTokens = new RouteValueDictionary()
            };

            if ((foldername != null) && (foldername.Length > 0))
            {
                route.DataTokens["Namespace"] = foldername;
            }

            routes.Add(name, route);

            return route;
        }

    }
}
